package pl.edu.agh.dosgi.logging;

import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
import org.osgi.service.log.LogReaderService;
import org.osgi.util.tracker.ServiceTracker;

import pl.edu.agh.dosgi.logging.listener.LogbackListener;
import pl.edu.agh.dosgi.logging.tracker.LogReaderTrackerCustomizer;
import pl.edu.agh.dosgi.logging.util.LogReadersHolder;


@SuppressWarnings("rawtypes")
public class Activator implements BundleActivator
{
    private LogbackListener logListener = new LogbackListener();
    private LogReaderTrackerCustomizer logReaderTrackerCustomizer;
    private LogReadersHolder logReadersHolder; 
	private ServiceTracker logReaderTracker;

	@SuppressWarnings("unchecked")
	public void start(BundleContext context) throws Exception
    {
    	logReadersHolder = new LogReadersHolder(logListener);
    	logReaderTrackerCustomizer = new LogReaderTrackerCustomizer(context, logListener, logReadersHolder);
    	
        logReaderTracker = new ServiceTracker(context, LogReaderService.class.getName(), logReaderTrackerCustomizer);
        logReaderTracker.open();
    }

    public void stop(BundleContext context) throws Exception
    {
    	logReaderTracker.close();
    	logReadersHolder.removeLogListenerFromReaders();
    }

}